package cn.yunyichina.scheduler.jobs.service.bone.impl;

import cn.yunyichina.provider.framework.dubbo.service.BoneService;
import cn.yunyichina.provider.iface.entity.bone.UserVistiListVo;
import cn.yunyichina.provider.iface.entity.common.Response;
import cn.yunyichina.scheduler.jobs.service.bone.BoneUserVisitPushService;
import cn.yunyichina.utils.convert.JsonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by zhuxp on 2017/12/6.
 *
 * @param
 * @Descrition
 */
@Service
public class BoneUserVisitPushServiceImpl implements BoneUserVisitPushService {
    private Logger logger = LoggerFactory.getLogger(BoneUserVisitPushService.class);

    @Override
    public void doExecute() {
        //获取当天的开始时间  跟结束时间
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String startTime = simpleDateFormat.format(new Date())+" 00:00:00";
        String endTime = simpleDateFormat.format(new Date())+" 23:59:59";
        //获取患者随访明细  随访时间 为当天  状态0 待完成
        UserVistiListVo userVistiListVo = new UserVistiListVo();
        userVistiListVo.setStatus((double) 0);
        userVistiListVo.setBeginVisitTime(startTime);
        userVistiListVo.setEndVisitTime(endTime);
        //获取患者随访明细列表
        logger.info("查询患者随访明细列表,请求yyt.bone.user.visit.detail.list入参:"+JsonUtils.toJsonString(userVistiListVo));
        Response response = BoneService.callHumpFromInternal("yyt.bone.user.visit.detail.list", JsonUtils.toJsonString(userVistiListVo));
        logger.info("查询患者随访明细列表,请求yyt.bone.user.visit.detail.list出参:"+JsonUtils.toJsonString(response.getResult()));
        if("0".equals(response.getResultCode())){
            List<UserVistiListVo> userVistiListVos =JsonUtils.parseArray(response.getResult(),UserVistiListVo.class);
            for(UserVistiListVo entity:userVistiListVos){
                logger.info("推送随访计划,请求yyt.bone.msg.user.visit.send入参:"+JsonUtils.toJsonString(entity));
                Response responsePush = BoneService.callHumpFromInternal("yyt.bone.user.visit.msg.send", JsonUtils.toJsonString(entity));
                logger.info("推送随访计划,请求yyt.bone.msg.user.visit.send出参:"+JsonUtils.toJsonString(responsePush));
                if ("0".equals(responsePush.getResultCode())){
                    logger.info("推送随访计划，推送成功!");
                }else{
                    logger.info("推送随访计划，推送失败!");
                }
            }
        }else if("-2".equals(response.getResultCode())){
            logger.info("不存在当天的患者随访明细！"+response.getResultMessage());
        }else{
            logger.error("查询患者随访明细列表异常！"+response.getResultMessage());
        }
    }
}
